import random

class SkillData:
    def __init__(self, name:str, stype:str, level1:int,level2:int):
        self.name = name
        self.stype = stype
        self.level1 = level1
        self.level2 = level2
def CopySkillData(skill1:SkillData, skill2:SkillData):
    skill1.name = skill2.name
    skill1.stype = skill2.stype
    skill1.level1 = skill2.level1
    skill1.level2 = skill2.level2

class SkillManager:
    def __init__(self,skill_filename:str,skilllevel_filename:str):
        levels = {}
        for line in open( "skilllevel.txt", encoding = "utf-8" ):
            line = line.rstrip('\n')
            if line[0] == '#':
                continue
            types = line.split('|')
            for level in range(len(types)):
                levels[types[level]] = level
        self.skills = {} #{ 技能名:skilldata}
        current_type = ''
        current_index = -1
        for line in open( "skill.txt", encoding = "utf-8" ):
            line = line.rstrip('\n')
            if line[0] == '#':
                continue
            ss = line.split('|')
            if current_type != ss[0]:
                current_type = ss[0]
                current_index = 0
            else:
                current_index +=1
            skillnames = ss[1].split(',')
            for skillname in skillnames:
                level1 = random.randint(1000, 1100)
                if current_type in levels:
                    level1 = levels[current_type]
                skill = SkillData(skillname,current_type,level1,current_index)
                self.skills[skillname] = skill
    def __CompareSkill(self,skill1:SkillData,skill2:SkillData)->int:
        if skill1.level1 == skill2.level1:
            if skill1.level2 < skill2.level2:
                return -1
            if skill1.level2 > skill2.level2:
                return 1
            return 0
        if skill1.level1 < skill2.level1:
            return -1
        if skill1.level1 > skill2.level1:
            return 1
        return 0
    def Choose(self, skillnames: list)->(int,[]):
        minSkill = SkillData("","",10000,10000)
        select = -1
        new_skillname = []
        for i in range(len(skillnames)):
            skillname = skillnames[i]
            skill = SkillData(skillname,"",1000,1000)
            if skillname not in self.skills:
                if skillname != None:
                    with open('skill_collect.txt', 'a',encoding='utf8') as f:
                        f.write(skillname+'\n')
                    skill.level1 = random.randint(1000, 1100)
                    skill.level2 = random.randint(1000, 1100)
                    new_skillname.append(skillname)
            else:
                skill :SkillData  = self.skills[skillname]

                if self.__CompareSkill(minSkill,skill) > 0:
                    CopySkillData(minSkill, skill)
                    select = i
        return (select,new_skillname)




